package com.pangolin.pangolinim.ui.base;

import android.app.Activity;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public class ActivityCollector {
    public static List<Activity> activities = new ArrayList<>();

    public static void addActivity(Activity activity){
        activities.add(activity);
    }

    public static void removeActivity(Activity activity){
        activities.remove(activity);
    }

    public static void finishAll(){
        for(Activity activity : activities){
            if(!activity.isFinishing()){
                activity.finish();
            }
        }
        activities.clear();
    }

    public static void finishAllExceptThis(Activity activity){
        Iterator<Activity> iterator = activities.iterator();
        while (iterator.hasNext()) {
            Activity item = iterator.next();
            if (!item.getClass().getSimpleName().equals(activity.getClass().getSimpleName())) {
                item.finish();
                iterator.remove();
            }
        }
    }
}
